/*** Class Name: IFulfillClaim
 * Author: Accenture
 * Date: 13-Jun-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: Invoked from custom 'CLAIM' button on FI list view. Selected FI records from the list view will be assigned to logged in user
 **/
public with sharing class IFulfillClaim {

    ApexPages.StandardSetController setCon;
    list<ID> FIList{get;set;}    
    public String finalURL{get;set;}
    string domain =null; 
    public String funtureCancellationDate{get;set;}  
    public String ownerNotPresentInQueue{get;set;}  
    public String ownerIsNotQueue{get;set;}    
    public String selectedFIID{get;set;} 
    
    public IFulfillClaim(ApexPages.StandardSetController stdSetController) {
      finalURL ='';   
      setCon = stdSetController;
      System.debug('setCon '+setCon);
      System.debug('ApexPages.currentPage()'+ApexPages.currentPage());

      String returnUrl = ApexPages.currentPage().getParameters().get('retUrl');
      finalURL = URL.getSalesforceBaseUrl().toExternalForm();
      finalURL = finalURL  + '/' + returnUrl;
      system.debug('finalURL ******'+finalURL);
    }
    /*
     * Selected Fulfillment Items Owner Changes to Logged in User.
     */
    public void assignFI(){   
      funtureCancellationDate ='';
      ownerNotPresentInQueue = '';
      ownerIsNotQueue = '';
      selectedFIID ='';
      List<Fullfilment_Item__c> claimFIList= new List<Fullfilment_Item__c>(); 
      List<Fullfilment_Item__c> fulfillmentList = new List<Fullfilment_Item__c>();
      List<Fullfilment_Item__c> finalFIList = new List<Fullfilment_Item__c>();  
      FIList= new list<ID>();
      for(sObject  s :setCon.getSelected()){ 
        Fullfilment_Item__c FI = (Fullfilment_Item__c)s;
        FIList.add(FI.ID);
        selectedFIID=FI.ID;
      } 
      if(FIList.size() ==0){
          return;
      }
      
      fulfillmentList =[select Name,id,transaction_type__c,Ownerid,Future_Cancellation__c from Fullfilment_Item__c where id IN :FIList];
      Map<ID,ID> groupMemberMap = new Map<ID,ID>();
      // Fetch the assoicate Queues for the logged user. 
       for(GroupMember GM:[Select g.GroupId, g.UserOrGroupId from GroupMember g where UserOrGroupId =:UserInfo.getUserId()]){
         groupMemberMap.put(GM.GroupId,GM.UserOrGroupId);
       }     
      System.debug('fulfillmentList  Size'+fulfillmentList.size()+'fulfillment'+fulfillmentList);
      System.debug('groupMemberMap size'+groupMemberMap.size()+groupMemberMap);
      
      for(Fullfilment_Item__c FI:fulfillmentList){
                
         //FutureCancellation Date+1day is greatet than Today     
         if((FI.Future_Cancellation__c!=null) && (FI.Future_Cancellation__c.date().addDays(1))>date.today()
              && FI.transaction_type__c.equalsignoreCase(IfullfillTextConstant.FI_CANCELLATION_TRANS_TYPE )){
              if(funtureCancellationDate.length()==0)
              {
                  funtureCancellationDate=FI.Name;
              }else{                
                  funtureCancellationDate+=','+FI.Name;
              }              
          }   
          //Checks if the Owner is user
          else if(((String)FI.Ownerid).startsWith(IfullfillTextConstant.OWNERVALUE)){
              if(FI.Ownerid != UserInfo.getUserId()){
                  //finalFIList.add(FI);
                  if(ownerIsNotQueue.length()==0)
                  {
                      ownerIsNotQueue=FI.Name;
                  }else{
                      ownerIsNotQueue+=','+FI.Name; 
                  }
              }              
          }//Person who is claimming the Items is part of Queue than add to list.
          else if(groupMemberMap.containsKey(FI.Ownerid)){
              finalFIList.add(FI);
          }
          //Person who is claimming the Items is not part of Queue than add to list.
          else{
              if(ownerNotPresentInQueue.length()==0)
              {
                  ownerNotPresentInQueue =FI.Name;
              }else{
                  ownerNotPresentInQueue+=','+FI.Name; 
              } 
          }      
      } 
      System.debug('finalFIList size'+finalFIList.size());
      for(Fullfilment_Item__c FI :finalFIList){ 
         FI.Ownerid= UserInfo.getUserId();
         System.debug('Ownerid '+FI.Ownerid);
      }
      try{
          if(finalFIList !=null && finalFIList.size()>0){
              update finalFIList;  
          }  
       }catch(DMLException e){
        GeneralConstants.mailAnyExceptionToMe('IFulfillClaim','assignFI',e);
       }   
    }

}